package com.triptogether.shoppinglist.fragments;

import java.util.Calendar;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.triptogether.R;
import com.triptogether.baseclasses.BaseFragment;
import com.triptogether.shoppinglist.adapters.ShoppingListsListAdapter;
import com.triptogether.shoppinglist.adapters.ShoppingListsListAdapter.OnItemSelectedListener;
import com.triptogether.trip.fragments.TripMainFragment;
import com.triptogether.utils.db.DBManager;
import com.triptogether.utils.db.models.ShoppingList;

public class ShoppingListsFragment extends BaseFragment {
	
	private ShoppingListsListAdapter shoppinglistListAdapter;
	private OnShoppingListSelectedListener shoppingListSelectedListener;
	private Long currentTripId;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    return inflater.inflate(R.layout.fragment_shopping_lists, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (savedInstanceState == null) {
			// Take the trip id from the arguments that sent with the fragment
			currentTripId = getArguments().getLong(TripMainFragment.ARG_TRIP_ID);
		} else {
			// Else, the savedInstanceState is not null (configuration changes...) than take the trip id from it
			currentTripId = savedInstanceState.getLong(TripMainFragment.ARG_TRIP_ID);
		}

		// Initialize the listView and its adapter
		final ListView lv = (ListView) getView().findViewById(R.id.listview_shopping_lists);
		
		// Init the adapter for the ShoppingList
		shoppinglistListAdapter = new ShoppingListsListAdapter(getActivity(), currentTripId);		
		shoppinglistListAdapter.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onShoppingListSelected(int position) {
				if (shoppingListSelectedListener != null) {
					ShoppingList shoppingList = (ShoppingList) lv.getItemAtPosition(position);
					shoppingListSelectedListener.ShoppingListSelected(shoppingList);
				}
			}
		});
		lv.setAdapter(shoppinglistListAdapter);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    
	    // Save the current Trip id
	    outState.putLong(TripMainFragment.ARG_TRIP_ID, currentTripId);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	    super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.shopping_lists_fragment_menu, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_item_add_shopping_list:
				// Create the shopping list in the DB
				ShoppingList newShoppingList = DBManager.getInstance(getActivity()).createShoppingList(
				        getString(R.string.new_shopping_list_default_name),
				        Calendar.getInstance().getTime(), currentTripId);
				
				shoppinglistListAdapter.add(newShoppingList);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	// /////////////////////
	// Listener
	// /////////////////////
	
	public interface OnShoppingListSelectedListener {
		public void ShoppingListSelected(ShoppingList selectedShoppingList);
	}
	
	public void setOnShoppingListSelectedListener(OnShoppingListSelectedListener listener) {
		this.shoppingListSelectedListener = listener;
	}
	
	// /////////////////////
	// Listener
	// /////////////////////
}
